<template>
  <div class="table">
    <div style="margin: 10px 0;padding: 10px 5px">
      <el-input style="width: 200px;padding-right: 5px" placeholder="请输入内容" v-model="name"></el-input>
      <el-button icon="el-icon-search" type="primary" @click="handleQuery">搜索</el-button>
    </div>
    <div style="margin: 10px 0;padding: 0 5px;display: flex">
      <el-popconfirm style="padding-left: 5px;padding-right: 5px" title="确认批量删除吗?" @confirm="delBatch">
        <template #reference>
          <el-button type="danger">批量删除</el-button>
        </template>
      </el-popconfirm>
      <el-button type="primary" icon="el-icon-top" @click="exp">导出</el-button>
      <el-button plain icon="el-icon-left" style="margin-left: auto;margin-right: 40px" @click="()=>$router.back()">返回
      </el-button>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="编辑" :visible.sync="editVisible" width="40%">
      <el-form label-width="90px" width="30%">
        <el-form-item label="乐谱名" prop="name">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>
        <el-form-item label="下载数量" prop="viewCounts">
          <el-input v-model="editForm.viewCounts"></el-input>
        </el-form-item>
        <el-form-item label="介绍" prop="introduction">
          <el-input v-model="editForm.introduction" type="textarea" maxlength="50"></el-input>
        </el-form-item>
        <el-form-item label="上传时间" prop="time">
          <el-date-picker type="date" value-format="yyyy-MM-dd" v-model="editForm.createTime"
                          maxlength="40"></el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>
      </span>
      </template>
    </el-dialog>

    <!--显示乐谱-->
    <el-table :data="tableData" stripe style="width: 100%;" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="序号" width="55" v-if="false"></el-table-column>
      <el-table-column prop="name" label="乐谱名" width="120"></el-table-column>
      <el-table-column prop="viewCounts" label="下载数量" width="90" align="center"></el-table-column>
      <el-table-column prop="introduction" label="介绍" align="center">
        <template slot-scope="scope" width="120%">
          <p style="height: 60px;line-height: 1.4em">{{ scope.row.introduction }}</p>
        </template>
      </el-table-column>
      <el-table-column prop="picture" label="封面" align="center">
        <template slot-scope="scope">
          <div>
            <img :src="scope.row.picture" alt="" style="width: 50px;height: 50px">
          </div>
          <el-upload ref="upload" :action="uploadUrl(scope.row.id)" :on-success="filesUploadSuccess">
            <el-button size="mini">更新图片</el-button>
          </el-upload>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="上传时间" align="center">
        <template slot-scope="scope">
          {{ attachBirth(scope.row.createTime) }}
        </template>
      </el-table-column>
      <el-table-column prop="staff" label="乐谱" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="success" @click="previewXml(scope.row.file)">乐谱显示</el-button>
        </template>
      </el-table-column>
      <!--按钮 -->
      <el-table-column label="操作" width="180px">
        <template slot-scope="scope">
          <el-button type="primary" @click="handleEdit(scope.row)" icon="el-icon-edit"></el-button>
          <el-popconfirm title="这是一段内容确定删除吗？" @confirm="handleDelete(scope.row.id)">
            <el-button slot="reference" type="danger" style="margin-left: 6px" icon="el-icon-delete"></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div style="padding: 10px 0;">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>

import {deleteStaff, deleteStaffs, songOfConsumerId, updateStaff} from "../api/index";
import {mixin} from "@/mixins";
import showXml from '../util/showxml'


export default {
  mixins: [mixin],
  name: "ConsumerStaff",
  data() {
    return {
      consumerId: '',//上传者id
      consumerName: '',//上传者姓名
      //查询
      tableData: [],
      name: '',//搜索
      total: 0,//分页，默认总数为0
      pageNum: 1,//分页，默认第一页
      pageSize: 5,//分页，默认一页有两条
      fileList: [],
      editForm: {
        name: '',
        username: '',
        viewCounts: '',
        picture: '',
        file: '',
        createTime: '',
        introduction: ''
      },
      editVisible: false,//默认不展示
      multipleSelection: [],//批量删除选中集合
    }
  },

  created() {
    this.consumerId = this.$route.query.id;
    this.consumerName = this.$route.query.name;
    this.getData();
  },
  //更新图片
  methods: {
    uploadUrl(id) {
      return `http://localhost:8888/staff/upload?id=${id}`
    },
    //更新成功之后
    filesUploadSuccess() {
      this.getData()//刷新表格
    },
    //查询乐谱
    getData() {
      this.tableData = [];//清空tableData里的数据
      songOfConsumerId(
        this.pageNum,
        this.pageSize,
        this.name,
        this.consumerId
      ).then(res => {
        const {records, total, pages} = res.data;
        this.tableData = records
        console.log({records})
        this.total = total
        if (this.pageNum > 1 && pages < this.pageNum) {
          console.log('当前页没有数据并且不是第一页是，重新请求前一页的数据')
          this.pageNum--;
          this.getData();
        }
      })
    },

    // 处理搜索按钮点击
    handleQuery() {
      this.pageNum = 1;
      this.getData();
    },
    //分页
    handleSizeChange(pageSize) {
      console.log(`每页 ${pageSize} 条`);
      this.pageSize = pageSize
      this.getData()//调用分页方法
    },
    handleCurrentChange(pageNum) {
      console.log(`当前页: ${pageNum}`);
      this.pageNum = pageNum
      this.getData()//调用分页方法
    },
    //操作----编辑
    handleEdit(row) {
      Object.assign(this.editForm, row) //把数据显示在表单上
      this.editVisible = true//打开弹窗
    },
    //编辑确定按钮
    saveEdit() {
      updateStaff(this.editForm).then(res => {
        if (res) {
          this.$message.success("修改成功")
          this.editVisible = false//关闭弹窗
          this.getData()//刷新数据
        } else {
          this.$message.error("修改失败")
          this.editVisible = false//关闭弹窗
        }
      })
    },
    //删除
    handleDelete(id) {
      deleteStaff(id).then(res => {
        if (res) {
          this.$message.success("删除成功")
          this.getData()
        } else {
          this.$message.success("删除失败")
        }
      })
    },
    //批量删除
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    //批量删除按钮
    delBatch() {
      let ids = this.multipleSelection.map(v => v.id)//把对象数组变成集合[{},{},{}] ----> [1,2,3]
      deleteStaffs(ids).then(res => {
        if (res) {
          this.$message.success("批量删除成功")
          this.getData()
        } else {
          this.$message.success("批量删除失败")
        }
      })
    },
    // 导出
    exp() {
      window.open("http://localhost:8888/staff/export")
    },
    //查看乐谱
    previewXml(fileUrl) {
      // 这里传入不同的url，展示不同滴文件
      showXml(fileUrl)
    }
  }


}
</script>

<style scoped>


</style>
